package com.sgcc.ln.pmos.e_ln_home.activity;


import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;

import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.ln.pmos.e_ln_home.adapter.VideoGuideAdapter;
import com.sgcc.ln.pmos.e_ln_home.bean.VideoGuideBean;
import com.sgcc.ln.pmos.e_ln_home.contract.VideoGuideContract;
import com.sgcc.ln.pmos.e_ln_home.presenter.VideoGuidePersenter;

import java.util.List;

import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videoplayer.player.VideoView;

@Route(path = HomeArouterPath.HOME_VIDEOGUIDE)
public class VideoGuideActivity extends MvpBaseRecycleActivity<VideoGuideBean, VideoGuidePersenter> implements VideoGuideContract.View {

    private VideoGuideAdapter mVideoGuideAdapter;
    private VideoView mPlayer;

    @Override
    protected VideoGuidePersenter createPresenter() {
        return new VideoGuidePersenter();
    }

    @Override
    public void queryVideoListSuccess(List<VideoGuideBean> list) {
        loadSuccess(false, list);
    }

    @Override
    public void queryVideoListFaile() {

    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mVideoGuideAdapter = new VideoGuideAdapter(mContext);
        mVideoGuideAdapter.setOnItemClickListener(new VideoGuideAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(int position, VideoGuideBean bean) {
                Log.e("TAG", "itemClickListener: " + bean.getVideoUrl());
                start(bean.getVideoUrl(), bean.getTitle());
            }
        });
        return mVideoGuideAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.getVideoList();
    }

    @Override
    public String getTitleText() {
        return "视频指南";
    }

    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_videoguide, parent, false);
        mPlayer = (VideoView) head_view.findViewById(R.id.player);
        String videoUrl = "https://exp-prod-sd.oss-cn-beijing.aliyuncs.com/public/2023/03/ln/ln000001.mp4";
        mPlayer.setUrl(videoUrl); //设置视频地址
        StandardVideoController controller = new StandardVideoController(this);
        controller.addDefaultControlComponent("title", false);
        mPlayer.setVideoController(controller); //设置控制器
        mPlayer.start();
        return head_view;
    }

    private void start(String videoUrl, String title) {
        if(mPlayer.isPlaying()){
            mPlayer.release();
        }
        mPlayer.setUrl(videoUrl); //设置视频地址
        StandardVideoController controller = new StandardVideoController(this);
        controller.addDefaultControlComponent(title, false);
        mPlayer.setVideoController(controller); //设置控制器
        mPlayer.start(); //开始播放，不调用则不自动播放
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mPlayer != null)
            mPlayer.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPlayer != null)
            mPlayer.resume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPlayer != null)
            mPlayer.release();
    }


}
